package com.jin.webvideo.base;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Map;

@Component
public class SpringBeanGetter implements ApplicationContextAware {
    private static ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (context == null) {
            context = applicationContext;
        }
    }
    public static<T> T getBean(Class<T> clazz) {
        Assert.notNull(context, "context is not injected wait for the injection");
        return context.getBean(clazz);
    }
    public static<T> T getBean(String name) {
        Assert.notNull(context, "context is not injected wait for the injection");
        return (T) context.getBean(name);
    }
    public static<T> Map<String, T> getBeansOfType(Class<T> clazz){
        return context.getBeansOfType(clazz);
    }
}
